var Api = require('../../utils/api.js');
var util = require('../../utils/util.js');
var Auth = require('../../utils/auth.js');

var wxApi = require('../../utils/wxApi.js')
var wxRequest = require('../../utils/wxRequest.js');
var app = getApp();
var webSiteName= config.getWebsiteName;
var domain =config.getDomain
import config from '../../utils/config.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    windowHeight:'100%',
    juanhao1:'扫卷号',
    juanhao2:'扫卷号',
    chanpin1:'',
    chanpin2:'',
    array2: ['-请选择车辆品牌-','奥迪', '宝马', '奔驰', '丰田'],
    cheliangpinpai:0,
    array3: ['-请选择汽车类型-','轿车','SUV', '大型SUV','MPV', '超跑'],
    leixing:0, //用于算价格
    chexing:'', 
    xiaohao1:'',
    xiaohao2:'',
    zhibaoneirong1:'',
    zhibaoneirong2:'',
    zhibao1:'0',
    zhibao2:'0',
    chepaihao:'', 
    chejiahao:'',
    chezhu:'',
    shoujihao:'',
    mendian:'',
    jiedai:'',
    jiage:'',
    shichangjia:'',
    radio:2,
    date:'-请选择日期-',
    size1:false,
    size2:false,
    size3:false,
    size4:false,
    size5:false,
    size6:false,
    size7:false,
    size8:false,
    size_two_1:false,
    size_two_2:false,
    size_two_3:false,
    size_two_4:false,
    size_two_5:false,
    size_two_6:false,
    size_two_7:false,
    size_two_8:false,
    shigongtu:'',
        // 展示的图片列表
        files: [],
        // 最多可上传的图片数量
        maxCount: {
          type: Number,
          value: 6,
        },
        // 单张图片的大小限制，单位 M
        size: {
          type: Number,
          value: 1,
        },
        // 图片大小类型。original：原图，comperssed：压缩图
        sizeType: {
          type: Array,
          value: ['original', 'comperssed'],
        },
        // 图片来源类型。album：从相册选图，camera：使用相机
        sourceType: {
          type: Array,
          value: ['album', 'camera'],
        }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //需要用户登录
    var self = this;
    wx.getSystemInfo({
      success: (result) => {
        self.setData({
          windowHeight:result.windowHeight,
          windowWidth:result.windowWidth
        })
        console.log(result.inFinChat);
        if(result.inFinChat)
        {          
          self.setData({inFinChat:result.inFinChat})          
        }
        else{          
          Auth.setUserInfoData(self);
          Auth.checkLogin(self);
          self.setData({copyright: getApp().globalData.copyright})
          Auth.checkSession(self, 'isLoginNow');
        }
      },
    })
    wx.request({
      url: 'https://a.ygtape.com.cn/index.php/qualityassurance/index/get_brand',
      method:'POST',
      header:{
        'content-type':'application/x-www-form-urlencoded'
      },
      success:function(res){
        res.data.data.unshift('-请选择车辆品牌-')
        self.setData({ 
          array2:res.data.data 
        })
      }
    })
    self.setData({
      mendian:this.data.userLevel.levelName
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  scanCode1:function(){
    var that = this
    var self = this
    wx.scanCode({
      scanType: ['barCode', 'qrCode', 'datamatrix', 'pdf417','pdf147'],
      success(res){
        let result = res.result;
        that.setData({
          juanhao1:result
        })
        wx.request({
          url: 'https://a.ygtape.com.cn/index.php/qualityassurance/index/get_product_info',
          method:'post',
          data:{
            reel_num:result,
            mobile:self.data.userInfo.user_mobile
          },
          success:function(res){ 
            if(res.data.status == 200){
              let new_data = res.data.data;
              self.setData({
                zhibao1:new_data.质保年,
                zhibaoneirong1:new_data.质保内容,
                chanpin1:new_data.产品,
              })
            }else{
     
            }
          }
        })

      },fail: (res) => {//扫码失败后
        console.log(res);
        wx.showToast({
            title: '扫码失败',
            icon: 'loading',
            duration: 500
        })
    },
    })
  }, 
  scanCode2:function(){
    var that = this
    var self = this
    wx.scanCode({
      scanType: ['barCode', 'qrCode', 'datamatrix', 'pdf417','pdf147'],
      success(res){
        let result = res.result;
        that.setData({
          juanhao2:result
        })
        wx.request({
          url: 'https://a.ygtape.com.cn/index.php/qualityassurance/index/get_product_info',
          method:'post',
          data:{
            reel_num:result,
            mobile:self.data.userInfo.user_mobile
          },
          success:function(res){ 
            console.log(res); 
            if(res.data.status == 200){
              let new_data = res.data.data;
              self.setData({
                zhibao2:new_data.质保年,
                zhibaoneirong2:new_data.质保内容,
                chanpin2:new_data.产品 
              })
            }else{
     
            }
          }
        })

      },fail: (res) => {//扫码失败后
        console.log(res);
        wx.showToast({
            title: '扫码失败',
            icon: 'loading',
            duration: 500
        })
    },
    })
  }, 
  size4Change:function(e){
      
    var target = 'size'+e.target.dataset.index;
    if(e.target.dataset.index == 2){
        //侧后挡
        this.setData({
            size3:e.detail,
            size4:e.detail,
            size5:e.detail,
            size6:e.detail,
            size7:e.detail,
        })
    }
    this.setData({
      [target]:e.detail
    })
  },
  sizeTwoChange:function(e){
    var target = 'size_two_'+e.target.dataset.index;

    if(e.target.dataset.index == 2){
        //侧后挡
        this.setData({
            size_two_3:e.detail,
            size_two_4:e.detail,
            size_two_5:e.detail,
            size_two_6:e.detail,
            size_two_7:e.detail,
        })
    }
    this.setData({
      [target]:e.detail
    })
  },
  bindPickerChange: function(e) {
    this.setData({
      chanpin: e.detail.value
    })
  },
  bindPickerChangeCheXing: function(e) {
      let that = this;
      that.setData({
        leixing: e.detail.value
    });
    
  },

  bindPickerChange2: function(e) {
    this.setData({
      cheliangpinpai: e.detail.value
    })
  },
  bindPickerChange3: function(e) {
    this.setData({
      chexing: e.detail.value
    })
  },
  bindPickerChange5: function(e) {
    this.setData({
      jiedai: e.detail.value
    })
  },
  uploadImage:function(e){
    var that = this;
    wx.chooseMedia({
      count: 9,
      mediaType: ['image'],
      sourceType: ['camera'],
      camera: 'back',
      success(res) {

        that.setData({
          shigongtu:res.tempFiles[0].tempFilePath
        })

      }
    })
  },
  tips_msg(msg,time = 1000,icon = 'none'){
    wx.showToast({
      icon: icon,
      title: msg,
      duration: time
    });
  }
  ,
  tijiao:function(e){
    //验证是否信息填写完毕
    var data = this.data;
    data.add_user_phone = this.data.userInfo.user_mobile;
    data.add_user = this.data.userInfo.nickName;
    if(!data.chanpin1){
      this.tips_msg('产品1为必填，请扫标识卡条形码！');
      return
    }
    if(!data.juanhao1){
      this.tips_msg('卷号1为必填，请扫标识卡条形码！');
      return
    }
    if(!data.xiaohao1){
      this.tips_msg('消耗米1为必填！',1500,'error');
      return
    }
    if(!data.chanpin2){
        this.tips_msg('产品2为必填，请扫标识卡条形码！');
        return
      }
      if(!data.juanhao2){
        this.tips_msg('卷号2为必填，请扫标识卡条形码！');
        return
      }
      if(!data.xiaohao2){
        this.tips_msg('消耗米2为必填！',1500,'error');
        return
      }


    if(data.cheliangpinpai == '0'){
      this.tips_msg('请选择车品牌！',1500,'error');
      return
    } 
    if(data.leixing == '0'){
        this.tips_msg('请选择汽车类型',1500,'error');
        return
    }


    // if(!data.chepaihao){
    //   this.tips_msg('车牌号为必填！',1500,'error');
    //   return 
    // }
    if(!data.chejiahao){
      this.tips_msg('车架号为必填！',1500,'error');
      return
    }
    if(data.date == '-请选择日期-'){
      this.tips_msg('请选择装贴日期',1500,'error');
      return
    }
    //附件必须四张图
    if(data.files.length < 4){
        this.tips_msg('装贴施工图必须大于4张！');
      return
    }
    data.token = this.data.userInfo.token,
    wx.request({
      url: 'https://a.ygtape.com.cn/index.php/qualityassurance/index/add2',
      method:'POST',
      data:data,
      header:{
        'content-type':'application/x-www-form-urlencoded'
      },
      success:function(res){
        if(res.data.status == 200){
            wx.showToast({title:'新增成功',duration: 2000});
            setTimeout(function(){ 
                wx.navigateTo({
                    url: '../warrantyManageList/manageList?mobile='+data.mobile+'&token='+data.token,
                }) 
            }, 1000);
        }else{
            wx.showToast({title:'操作失败，请检查字段是否有错误',duration: 2000});
        }
      }
    })

  },

  handleChooseImage:function(e){

    var that = this;

    // 选择图片
    wx.chooseMedia({
      count: 6,
      mediaType: ['image'],
      sourceType: ['album','camera'],
      camera: 'back',
      success(res) {

        res.tempFiles.forEach(element => {
            wx.uploadFile({
            url:'https://a.ygtape.com.cn/index.php/qualityassurance/index/upload', //仅为示例，非真实的接口地址
            filePath:element.tempFilePath,
            name: 'file', 
            formData: {
                'mobile': that.data.userInfo.user_mobile,
                'token':that.data.userInfo.token,
            },
            success: function (res) {
                console.log(res);
                console.log(res.data);
                res.data = JSON.parse(res.data);
                console.log(res.data.status);
                if (res.data.status == 200) {
                that.setData({
                    files: [...that.data.files, res.data.url]
                })
                }else{
                wx.showToast({
                    title: '上传失败',
                    icon:'error',
                    duration: 1000
                });
                } 
            }
            })
        });
      
      }
    })

  },

  // 预览图片
  handlePreviewImage: function(e) {
    const {index} = e.currentTarget.dataset
    const {files} = this.data
    wx.previewImage({
      urls: files,
      current: files[index],
    })
  },

  // 删除图片
  handleDeleteImage: function(e) {
    const {index} = e.currentTarget.dataset
    let files = [...this.data.files]
    files.splice(index, 1)
    this.setData({files})
  },
  bindDateChange:function(e){
    this.setData({
      date: e.detail.value
    })
  },
  getinputVlue:function(e){
    var self = this;
    var name = e.currentTarget.dataset.name;
    self.setData({
      [name]:e.detail.value
    })
  }



})